<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="750px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @deleteCancel="deleteCancel" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <div class="formTopFont">点检计划设置</div>
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto">
        <el-row>
          <el-col :span="24">
            <el-form-item label="计划名称" prop="planName">
              <el-input v-model="ruleForm.planName" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="计划编码" prop="planCode">
              <el-input v-model="ruleForm.planCode" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="点检频率" prop="pointFrequency">
              <el-select v-model="ruleForm.pointFrequency" style="width: 100%">
                <el-option v-for="(item,index) in frequencyList" :key="index" :value="item.value" :label="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="点检对象" prop="equipIdList">
              <el-select v-model="ruleForm.equipIdList" multiple style="width: 100%">
                <el-option v-for="item in eqAllList" :key="item.id" :label="item.equipName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
  </el-dialog>
</template>
<script>
import { getEqListApi } from '@/api/public'
export default {
  props: {
    deptAllList: {
      type: Array,
      default: () => {
        return []
      }
    },
    userAllList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    const checkPlanCode = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/equip/point/plan/verifyCodeUnique?planCode=${value}&id=${
          this.ruleForm.id || ''
        }`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('计划编码重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      frequencyList: [
        {
          value: '1小时',
          id: '1小时',
          label: '1小时'
        },
        {
          value: '2小时',
          id: '2小时',
          label: '2小时'
        },
        {
          value: '3小时',
          id: '3小时',
          label: '3小时'
        },
        {
          value: '4小时',
          id: '4小时',
          label: '4小时'
        },
        {
          value: '5小时',
          id: '5小时',
          label: '5小时'
        },
        {
          value: '6小时',
          id: '6小时',
          label: '6小时'
        },
        {
          value: '7小时',
          id: '7小时',
          label: '7小时'
        },
        {
          value: '8小时',
          id: '8小时',
          label: '8小时'
        },
        {
          value: '10小时',
          id: '10小时',
          label: '10小时'
        },
        {
          value: '12小时',
          id: '12小时',
          label: '12小时'
        },
        {
          value: '24小时',
          id: '24小时',
          label: '24小时'
        },
        {
          value: '48小时',
          id: '48小时',
          label: '48小时'
        },
        {
          value: '每周',
          id: '每周',
          label: '每周'
        }
      ],
      ruleForm: {
        planCode: '',
        pointHour: '',
        pointFrequency: '24小时',
        equipIdList: [],
        planName: ''
      },
      rules: {
        planCode: [
          { required: true, message: '计划编码不能为空！', trigger: 'blur' },
          { validator: checkPlanCode, trigger: 'blur' }
        ],
        equipIdList: [
          {
            type: 'array',
            required: true,
            message: '请选择设备',
            trigger: 'change'
          }
        ],
        pointFrequency: [
          { required: true, message: '请选择点检频率！', trigger: 'change' }
        ]
      },
      dialogVisible: false,
      eqAllList: [],
      obj: {}
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      this.obj = row
      this.getEqInfo()
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        const res = await this.$axios.get(
          `/equip/point/plan/detail?id=${row.id}`
        )
        if (res?.data.code === 200) {
          this.ruleForm = Object.assign({}, res.data.data)
        }
      } else {
        this.title = '新增'
      }
      this.dialogVisible = true
    },
    /* 获取所有设备 */
    async getEqInfo() {
      const res = await getEqListApi()
      if (res?.data.code === 200) {
        this.eqAllList = res.data.data
      }
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.$axios.post(
            '/equip/point/plan/submit',
            this.ruleForm
          )
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    async deleteCancel() {
      if (this.title === '编辑') {
        this.$confirm(`确认删除数据？`, '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$axios
              .delete(`/equip/point/plan/remove?id=${this.obj.id}`)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$notify.success('删除成功！')
                  this.dialogVisible = false
                  this.$emit('getTableInfo', 1)
                } else {
                  this.$notify.error(res.data.msg)
                }
              })
          })
          .catch(() => {})
      }
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        planCode: '',
        pointHour: '',
        pointFrequency: '',
        equipIdList: [],
        planName: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
